<?php

namespace App\Jobs;

use App\Models\Api\Interview;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use JPush\Client as JPush;
use Site;

//app用户收到面试邀请的远程推送
class PersonalInterViewApiPush extends Job implements ShouldQueue
{
    use InteractsWithQueue, SerializesModels;

    protected $user;
    protected $companyJobData;

    public function __construct($user, $companyJobData)
    {
        $this->user = $user;
        $this->companyJobData = $companyJobData;
    }

    public function handle()
    {
        $this->config();
        if (is_null($this->user) || is_null($this->companyJobData)) {
            return;
        }

        //获取当前面试记录
        $result = Interview::where('id', $this->companyJobData->interview_id)->first(['status']);

        if (is_null($result) || $result->status != 0) {//没有被查看的,才发送
            return;
        }

        //配置推送项
        $app_key = Site::get('personal_app_key');
        $master_secret = Site::get('personal_master_secret');
        //第三个参数是log文件路径,手动指定为null
        $client = new JPush($app_key, $master_secret, null);

        $pusher = $client->push();

        $isAndroid = true;
        //推送平台设置,目前支持 ios, android, winphone
        //RegistrationId可辨别 Android&iOS:从左数，第三位代表平台 0 代表 Android 1 代表 iOS
        if (substr($this->user->device_token, 2, 1) == 1) {
            $isAndroid = false;
        }
        $pusher->setPlatform($isAndroid ? 'android' : 'ios');

        //推送设备对象，表示一条推送可以被推送到哪些设备列表。
        //确认推送设备对象，JPush 提供了多种方式，比如：别名、标签、注册ID、分群、广播等。
        //在客户端监听广播，或调用接口 获取到 RegistrationID，然后将其保存到己方服务器，以备使用！
        $pusher->addRegistrationId($this->user->device_token);//目标人群

        $title = '来自『' . $this->companyJobData->company_name . '』的面试邀请';

        $body = '@' . $this->companyJobData->name . ',您好!『'
            . $this->companyJobData->company_name . '』邀请您参加『'
            . $this->companyJobData->job_title . '』面试,请及时查看';

        $notification = [
            'sound' => 'sound.caf',//设置声音
            'badge' => '+1',//红色角标
//            'content-available' => true,
//            'mutable-content' => true,
//            'category' => 'jiguang',
//            'extras' => array(
//                'key' => 'value',
//                'jiguang'
//            ),
        ];
        if ($isAndroid) {
            $pusher->androidNotification($body, $notification + ['title' => $title]);
        } else {
            $pusher->iosNotification([
                'title' => $title,
//                'subTitle'=>'subTitle',//subtitle IOS 10 才支持
                'body' => $body,
            ], $notification);

            $pusher->options([
                'apns_production' => true,//设置是开发环境还是线上环境
            ]);
        }

        try {
            $response = $pusher->send();
//            \Log::info('推送结果=' . json_encode($response));
        } catch (\JPush\Exceptions\JPushException $e) {
            //出错了
//            \Log::debug('推送出错了=' . $e);
        }
    }
}
